﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class ExchangeManage : MarshalByRefObject
    {
        private static readonly ExchangeDal Dal = new ExchangeDal();

        /// <summary>
        /// 金币转换点数
        /// </summary>
        /// <param name="coin"></param>
        /// <returns></returns>
        [Logging("{user}金币转换点数。"), ExceptionCall]
        public Result Coin2Point(int coin)
        {
            Result res = new Result();
            if (coin < Dal.Check("Coin", UserManage.Current.UserID))
            {
                res.Errors = "超出现存量！";
                return res;
            }

            CMSConfig config = CMSConfig.Instance();

            double d = coin*config.Member.JingBiHuiLv;
            Dal.Coin2Point(UserManage.Current.UserID, coin, MyMath.GetInteger(d));

            return res;
        }

        /// <summary>
        /// 点数转换金币
        /// </summary>
        /// <param name="point"></param>
        [Logging("{user}点数转换金币。"), ExceptionCall]
        public Result Point2Coin(int point)
        {
            Result res = new Result();
            if (point < Dal.Check("Point", UserManage.Current.UserID))
            {
                res.Errors = "超出现存量！";
                return res;
            }
            CMSConfig config = CMSConfig.Instance();
            double d = point*(1/config.Member.JingBiHuiLv);
            Dal.Point2Coin(UserManage.Current.UserID, point, MyMath.GetInteger(d));

            return res;
        }

        /// <summary>
        /// 经验值转换点数
        /// </summary>
        /// <param name="experience"></param>
        [Logging("{user}经验值转换点数。"), ExceptionCall]
        public Result Experience2Point(int experience)
        {
            Result res = new Result();
            if (experience < Dal.Check("Experience", UserManage.Current.UserID))
            {
                res.Errors = "超出现存量！";
                return res;
            }
            CMSConfig config = CMSConfig.Instance();
            double d = experience*(1/config.Member.JianYanHuiLv);
            Dal.Experience2Point(UserManage.Current.UserID, experience, MyMath.GetInteger(d));


            return res;
        }

        /// <summary>
        /// 金币赠与
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="coin"></param>
        [Logging("{user}金币赠与{@p0}，金币{@p1}。"), ExceptionCall]
        public Result CoinGive(string userName, int coin)
        {
            Result res = new Result();
            bool b = new MemberManage().CheckUserName(userName);
            if (b == false)
            {
                res.Errors = "用户名错误，操作失败。";
                return res;
            }
            Dal.CoinGive(userName, UserManage.Current.UserName, coin);
            return res;
        }
    }
}